<?php
if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );
class StockOrder_model extends MY_Model {
	var $table = 'stock_order';
	
	// 构造函数
	public function __construct() {
		// 调用父类构造函数，必不可少
		parent::__construct ();
	}
	
	/**
	 * 获取所有单
	 *
	 * @param array $config        	
	 * @param array $like        	
	 */
	public function getFillStock($config = array(), $like = array(), $type) {		
		$config['field'] = 's.id';
		$config['select'] = 's.status,s.id,s.order_num,s.request_total_num,ae.address,s.created_at';
		$config['join'] = array(
				array('areas_enabled ae', 's.address_id=ae.id', 'left'),
		);
		$config['totalCount'] = $this->get_search_num(NULL, $like, $config, ' s');
		if($config['offset'] >= $config['totalCount']) {
			$config['offset'] = 0;
			$config['pageNum'] = 1;
		}
		$config['data'] = $this->search(array('s.type' =>$type), $config, $like, false, ' s');
		return $config;
	}
	
	/**
	 * 查找详细信息(查找订单单条信息)
	 *
	 * @param integer $id        	
	 * @return array
	 */
	public function findStockOrder($id) {
		$query = $this->db->select ( 's.status,s.id,s.order_num,s.request_total_num,ae.address,s.created_at' )->from ( $this->table . ' s' )->join ( 'areas_enabled ae', 's.address_id=ae.id', 'left' )->where ( array (
				's.id' => $id 
		) )->get ();
		return $query->row_array ();
	}
	
	/**
	 * 查找详细信息(查找对应订单下的所有请求商品)
	 *
	 * @param integer $id        	
	 * @return array
	 */
	public function findOrderProduct($id) {
		$query = $this->db->select ( 'p.name,p.stock,p.price,sa.request_number,sa.actual_number,sa.product_id,sa.id' )->from ( 'stock_assignment sa' )->join ( 'product p', 'sa.product_id=p.id', 'left' )->where ( array (
				'sa.order_id' => $id 
		) )->get ();
		$data ['data'] = $query->result_array ();
		return $query->result_array ();
	}
	
	/**
	 * 更新补仓状态(更新补仓单状态与更新记录)
	 *
	 * @param unknown $data        	
	 * @param unknown $oid        	
	 */
	public function upFillOrder($data, $oid, $status) {
		try {
			$this->db->trans_begin ();
			$this->db->where ( array (
					'id' => $oid 
			) );
			$num = $this->db->update ( 'stock_order', array (
					'status' => $status 
			) );
			foreach ( $data as $v ) {
				$this->db->set ( 'actual_number', "{$v['actual_number']}", false );
				$this->db->where ( array (
						'id' => $v ['id'] 
				) );
				
				$this->db->update ( 'stock_assignment' );
			}
			$this->db->trans_commit ();
			return true;
		} catch ( Exception $e ) {
			$this->db->trans_rollBack ();
			return false;
		}
	}
	
	/**
	 * 更新退仓订单状态
	 *
	 * @param array $data        	
	 * @param unknown $oid        	
	 */
	public function upBackOrderStatus($data, $oid) {
		return $this->update ( $data, array (
				'id' => $oid 
		) );
	}
	
	/**
	 * 更新补仓状态(更新补仓单状态与更新记录)
	 *
	 * @param unknown $data        	
	 * @param unknown $oid        	
	 */
	public function upBackOrder($data, $oid, $status) {
		try {
			$this->db->trans_begin ();
			
			// 更新订单状态
			$this->db->where ( array (
					'id' => $oid 
			) );
			$num = $this->db->update ( 'stock_order', array (
					'status' => $status 
			) );
			
			foreach ( $data as $v ) {
				
				// 总仓数据库增加
				$this->db->set ( 'stock', "stock+{$v['actual_number']}", false );
				$this->db->where ( array (
						'id' => $v ['product_id'] 
				) );
				
				$this->db->update ( 'product' );
				
				// 更新实际收货数量
				$this->db->set ( 'actual_number', "{$v['actual_number']}", false );
				$this->db->where ( array (
						'id' => $v ['id'] 
				) );
				
				$this->db->update ( 'stock_assignment' );
			}
			$this->db->trans_commit ();
			return true;
		} catch ( Exception $e ) {
			$this->db->trans_rollBack ();
			return false;
		}
	}
}
